package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.mall.MallCart;
import com.tencreat.service.mall.IMallCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 购物车商品Controller
 *
 * @author tencreat
 * @date 2021-08-15
 */
@Api(tags="购物车商品api")
@RestController
@RequestMapping("/mall/cart")
public class AdminMallCartController extends BaseController {
    @Autowired
    private IMallCartService mallCartService;

    /**
     * 查询购物车商品列表
     */
    @ApiOperation(value="查询购物车商品列表")
    @PreAuthorize("@ss.hasPermi('mall:cart:list')")
    @PostMapping("/list")
    public HttpResult<IPage<MallCart>> list(@RequestBody  MallCart mallCart) {
        QueryWrapper<MallCart> wrapper = Wrappers.query(mallCart);
        Page<MallCart> page =  new Page<>(mallCart.getPageNum(), mallCart.getPageSize());
        return HttpResult.ok(mallCartService.page(page, wrapper));
    }

    /**
     * 导出购物车商品列表
     */
    @ApiOperation(value="导出购物车商品列表")
    @PreAuthorize("@ss.hasPermi('mall:cart:export')")
    @Log(title = "购物车商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(MallCart mallCart) {
        List<MallCart> list = mallCartService.selectMallCartList(mallCart);
        ExcelUtil<MallCart> util = new ExcelUtil<MallCart>(MallCart.class);
        return util.exportExcel(list, "cart");
    }

    /**
     * 获取购物车商品详细信息
     */
    @ApiOperation(value="获取购物车商品详细信息")
    @PreAuthorize("@ss.hasPermi('mall:cart:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<MallCart> getInfo(@RequestBody   ReqIdParam id) {
        return HttpResult.ok(mallCartService.selectMallCartById(id));
    }

    /**
     * 新增购物车商品
     */
    @ApiOperation(value="新增购物车商品")
    @PreAuthorize("@ss.hasPermi('mall:cart:add')")
    @Log(title = "购物车商品", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  MallCart mallCart) {
        return to(mallCartService.insertMallCart(mallCart));
    }

    /**
     * 修改购物车商品
     */
    @ApiOperation(value="修改购物车商品")
    @PreAuthorize("@ss.hasPermi('mall:cart:edit')")
    @Log(title = "购物车商品", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  MallCart mallCart) {
        return to(mallCartService.updateMallCart(mallCart));
    }

    /**
     * 删除购物车商品
     */
    @ApiOperation(value="删除购物车商品")
    @PreAuthorize("@ss.hasPermi('mall:cart:remove')")
    @Log(title = "购物车商品", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(mallCartService.deleteMallCartByIds(ids));
    }
}
